package org.tis.tools.abf.module.om.controller;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.om.controller.request.OmEmployeeAddRequest;
import org.tis.tools.abf.module.om.controller.request.OmEmployeeByOrgAndPositionRequest;
import org.tis.tools.abf.module.om.controller.request.OmEmployeeMajorsRequest;
import org.tis.tools.abf.module.om.controller.request.OmEmployeeUpdateRequest;
import org.tis.tools.abf.module.om.entity.OmEmployee;
import org.tis.tools.abf.module.om.entity.enums.OmEmployeeStatus;
import org.tis.tools.abf.module.om.service.IOmEmployeeService;
import org.tis.tools.core.utils.StringUtil;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.PageVO;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * omEmployee的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/omEmployee")
@Validated
@Slf4j
public class OmEmployeeController extends BaseController<OmEmployee>  {

    @Autowired
    private IOmEmployeeService omEmployeeService;


    @ApiOperation(value = "新增员工",notes = "传入OmEmployeeAddRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增员工")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "OmEmployeeAddRequest",name = "传入的OmEmployeeAddRequest类",required = true) OmEmployeeAddRequest omEmployeeAddRequest) {
        if (omEmployeeService.queryEmpCode(omEmployeeAddRequest.getEmpCode())){
            return ResultVO.error("404","员工代码已存在,请重新输入!");
        }

        omEmployeeService.add(omEmployeeAddRequest);
        return ResultVO.success("新增成功！");
    }


    @ApiOperation(value = "修改员工",notes = "传入OmEmployeeUpdateRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改员工")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "OmEmployeeUpdateRequest",name = "传入的OmEmployeeUpdateRequest类",required = true) OmEmployeeUpdateRequest omEmployeeUpdateRequest) {
        OmEmployee omEmployee = omEmployeeService.selectById(omEmployeeUpdateRequest.getGuid());
        if (omEmployee == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (omEmployeeService.queryEmpCodeForUpdate(omEmployeeUpdateRequest.getEmpCode(),omEmployeeUpdateRequest.getGuid())){
            return ResultVO.error("404","员工代码已存在,请重新输入!");
        }

        omEmployee = omEmployeeService.change(omEmployeeUpdateRequest);
        return ResultVO.success("修改成功！",omEmployee);
    }


    @ApiOperation(value = "删除员工",notes = "传入员工GUID(id)作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除员工")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "员工GUID",required = true) String id) {
        OmEmployee omEmployee = omEmployeeService.selectById(id);
        if (omEmployee == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omEmployeeService.moveEmp(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据ID查询员工",notes = "传入员工GUID(id)作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "员工GUID",required = true) String id) {
        OmEmployee omEmployee = omEmployeeService.selectById(id);
        if (omEmployee == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", omEmployee);
    }


    @ApiOperation(value = "分页查询所有员工",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息",required = true) SmartPage<OmEmployee> page) {
        return  ResultVO.success("查询成功", omEmployeeService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "根据机构ID分页查询员工",notes = "传入分页信息和机构GUID(id)作为参数")
    @PostMapping("/queryEmpByOrg/{id}")
    public ResultVO queryEmpByOrg(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息",required = true) SmartPage<OmEmployee> page,@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "机构GUID",required = true) String id){
        return ResultVO.success("查询成功",omEmployeeService.queryEmpByOrg(page.getPageObj(),page.getConditionWrapper(),id));
    }


    @ApiOperation(value = "根据机构ID不分页查询员工",notes = "传入机构GUID(id)作为参数")
    @GetMapping("/listsByOrg/{id}")
    public ResultVO queryEmpByOrg(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "机构GUID",required = true) String id){

        List<OmEmployee> list = omEmployeeService.queryEmpListByOrg(id);
        return ResultVO.success("查询成功",list);
    }


    @ApiOperation(value = "根据机构和岗位ID分页查询员工信息",notes = "传入分页信息(page)作为参数")
    @PostMapping("/queryByOrgPosition")
    public ResultVO queryByOrgPosition(@RequestBody @Validated @ApiParam(value = "page",name = "自定义的分页信息",required = true) SmartPage<OmEmployeeByOrgAndPositionRequest> page){
            return ResultVO.success("查询成功",omEmployeeService.queryByOrgPosition(getThisPage(page),page.getCondition().getGuidOrg(),page.getCondition().getGuidPosition()));
    }

    /**
     * 为根据机构和岗位ID分页查询员工信息接口写的获取page的方法
     */
    private Page<OmEmployee> getThisPage(SmartPage<OmEmployeeByOrgAndPositionRequest> smartPage) {
        PageVO vo = smartPage.getPage();
        Page<OmEmployee> page = new Page<>(vo.getCurrent(), vo.getSize());
        if (vo.getOrderByField() != null) {
            page.setOrderByField(vo.getOrderByField());
            page.setAsc(vo.getAsc());
        }
        return page;
    }


    @ApiOperation(value = "获取同机构下不在某岗位下的员工",notes = "传入OmEmployeeByOrgAndPositionRequest类作为参数")
    @PostMapping("/otherEmployee")
    public ResultVO getOtherEmp(@RequestBody @Validated @ApiParam(value = "OmEmployeeByOrgAndPositionRequest",name = "传入的OmEmployeeByOrgAndPositionRequest类",required = true) OmEmployeeByOrgAndPositionRequest om){
        return ResultVO.success("查询成功",omEmployeeService.getOtherEmp(om));
    }


    @ApiOperation(value = "员工入职",notes = "传入OmEmployee类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "员工入职")
    @PutMapping("/onJob")
    public ResultVO onJob(@RequestBody @Validated @ApiParam(value = "OmEmployee",name = "传入的OmEmployee类",required = true) OmEmployee omEmployee) {
        OmEmployee omEmployeeQue = omEmployeeService.selectById(omEmployee.getGuid());
        if (omEmployeeQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (OmEmployeeStatus.OFFER != omEmployeeQue.getEmpstatus()){
            return ResultVO.error("404", "员工不是在招状态不能办理入职");
        }

        omEmployeeQue = omEmployeeService.onJob(omEmployee);
        return ResultVO.success("入职成功！",omEmployeeQue);
    }


    @ApiOperation(value = "修改员工入职",notes = "传入OmEmployee类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改员工入职")
    @PutMapping("/changeOnJob")
    public ResultVO changeOnJob(@RequestBody @Validated @ApiParam(value = "OmEmployee",name = "传入的OmEmployee类",required = true) OmEmployee omEmployee){
        OmEmployee omEmployeeQue = omEmployeeService.selectById(omEmployee.getGuid());
        if (omEmployeeQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (OmEmployeeStatus.ONJOB != omEmployeeQue.getEmpstatus()){
            return ResultVO.error("404", "员工不是入职状态不能修改");
        }
        omEmployeeQue = omEmployeeService.changeOnJob(omEmployee);
        return ResultVO.success("修改成功！",omEmployeeQue);
    }


    @ApiOperation(value = "员工离职",notes = "传入OmEmployee类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "员工离职")
    @PutMapping("/outJob")
    public ResultVO outJob(@RequestBody @Validated @ApiParam(value = "OmEmployee",name = "传入的OmEmployee类",required = true) OmEmployee omEmployee) {
        OmEmployee omEmployeeQue = omEmployeeService.selectById(omEmployee.getGuid());
        if (omEmployeeQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (OmEmployeeStatus.ONJOB != omEmployeeQue.getEmpstatus()){
            return ResultVO.error("404", "员工不是在职状态不能办理离职");
        }

        omEmployeeQue = omEmployeeService.outJob(omEmployee);
        return ResultVO.success("离职成功！",omEmployeeQue);
    }


    @ApiOperation(value = "为岗位/机构和员工的机构表里添加员工",notes = "传入OmEmployee类作为参数")
    @PostMapping("/addByOrgPosition")
    public ResultVO addInOrgAndPosition(@RequestBody @Validated @ApiParam(value = "OmEmployee",name = "传入的OmEmployee类",required = true) OmEmployee omEmployee){
        omEmployeeService.addInOrgAndPosition(omEmployee);
        return ResultVO.success("添加成功");
    }


    @ApiOperation(value = "根据姓名查询员工分页",notes = "传入分页信息作为参数")
    @PostMapping("/queryByName")
    public ResultVO queryByName(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息",required = true) SmartPage<OmEmployee> page){
        return  ResultVO.success("查询成功", omEmployeeService.queryEmpByName(page.getPageObj(),page.getConditionWrapper()));
    }


    @ApiOperation(value = "根据机构查询已入职的员工",notes = "传入机构GUID(orgId)作为参数")
    @GetMapping("/queryEmp/{orgId}")
    public ResultVO queryEmp(@PathVariable @NotBlank(message = "orgId不能为空") @ApiParam(value = "orgId",name = "机构GUID", required = true) String orgId){
        return ResultVO.success("查询成功",omEmployeeService.queryEmp(orgId));
    }


    @ApiOperation(value = "查询所有已入职的员工",notes = "传入角色GUID(roleId)作为参数")
    @GetMapping("/queryEntryEmp/{roleId}")
    public ResultVO queryEntryEmp(@PathVariable @NotBlank(message = "角色GUID不能为空") @ApiParam(value = "roleId",name = "角色GUID",required = true) String roleId){
        return ResultVO.success("查询成功",omEmployeeService.queryEntryEmp(roleId));
    }


    @ApiOperation(value = "根据机构查询角色下已分配和未分配的员工",notes = "传入机构GUID(orgId)和角色GUID(roleId)作为参数")
    @GetMapping("/queryEmployeesByOrgId/{orgId}/{roleId}")
    public ResultVO queryEmployeesByOrgId(@PathVariable @NotBlank(message = "orgId不能为空") @ApiParam(value = "orgId",name = "机构GUID", required = true) String orgId, @PathVariable @NotBlank(message = "roleId不能为空") @ApiParam(value = "roleId",name = "角色GUID",required = true) String roleId){
        return ResultVO.success("查询成功",omEmployeeService.queryEmployeesByOrgId(orgId,roleId));
    }

    @ApiOperation(value = "检查新增时员工代码是否已经存在",notes = "传入员工代码(empCode)参数")
    @GetMapping("/queryEmpCode/{empCode}")
    public ResultVO queryEmpCode(@PathVariable @NotBlank(message = "员工代码不能为空") @ApiParam(name = "empCode" ,value =
            "员工代码",required = true) String empCode){
        return ResultVO.success("查询成功",omEmployeeService.queryEmpCode(empCode));
    }

    @ApiOperation(value = "检查修改时员工代码是否已经存在",notes = "传入员工代码(empCode)和GUID(id)参数")
    @GetMapping("/{id}/existUpdateEmpCode/{empCode}")
    public ResultVO getUpdateEmpCode(@PathVariable @NotBlank(message = "员工代码不能为空") @ApiParam(name = "empCode" ,value = "员工代码",required = true) String empCode,@PathVariable @NotBlank(message = "GUID不能为空") @ApiParam(name = "id" ,value = "员工GUID",required = true) String id){
        return ResultVO.success("查询成功",omEmployeeService.queryEmpCodeForUpdate(empCode,id));
    }

    @ApiOperation(value = "查询可分配给岗位的员工",notes = "传入机构GUID(guidOrg)参数")
    @GetMapping("/{guidOrg}/getAssignableEmployee")
    public ResultVO getAssignableEmployee(@PathVariable @NotBlank(message = "机构GUID不能为空") @ApiParam(name = "guidOrg" ,value =
            "机构GUID",required = true) String guidOrg){
        return ResultVO.success("查询成功",omEmployeeService.queryAssignableEmployee(guidOrg));
    }

    @ApiOperation(value = "查询员工的可分配直接主管",notes = "传入机构GUID(guidOrg)和GUID(guid)参数")
    @PostMapping("/getEmployeeMajors")
    public ResultVO getEmployeeMajors(@RequestBody @Validated @ApiParam(name = "OmEmployeeMajorsRequest" ,value = "传入参数OmEmployeeMajorsRequest",required = true) OmEmployeeMajorsRequest omEmployeeMajorsRequest){
        if (StringUtil.isNotEmpty(omEmployeeMajorsRequest.getGuid())){
            OmEmployee omEmployeeQue = omEmployeeService.selectById(omEmployeeMajorsRequest.getGuid());
            if (omEmployeeQue == null) {
                return ResultVO.error("404", "找不到对应记录或已经被删除！");
            }
        }
        return ResultVO.success("查询成功",omEmployeeService.getEmployeeMajors(omEmployeeMajorsRequest.getGuidOrg(),
                omEmployeeMajorsRequest.getGuid()));
    }
}

